package com.yvtian.pu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.pu.pojo.PuGoodsReceiptEntity;
import com.yvtian.pu.pojo.PuOrderEntity;
import com.yvtian.pu.dao.PuOrderMapper;
import com.yvtian.pu.pojo.PuOrderShopEntity;
import com.yvtian.pu.service.PuOrderService;
import com.yvtian.pu.service.PuOrderShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * 采购单(pu_order)表服务实现类
 *
 * @author long
 * @since 2024-06-24 10:26:28
 */
@Service("puOrderService")
public class PuOrderServiceImpl extends ServiceImpl<PuOrderMapper, PuOrderEntity> implements PuOrderService {

    @Autowired
    private PuOrderShopService puOrderShopService;

    @Override
    public IPage<PuOrderEntity> findByPage(Page<PuOrderEntity> page, PuOrderEntity puOrderEntity) {
        LambdaQueryWrapper<PuOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PuOrderEntity::getIsDel, 0);
        getQueryWrapper(puOrderEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(PuOrderEntity puOrderEntity) {
        ArrayList<PuOrderShopEntity> orderShopEntityList = puOrderEntity.getOrderShopEntityList();
        if (orderShopEntityList != null && !orderShopEntityList.isEmpty()) {
            LambdaQueryWrapper<PuOrderShopEntity> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shopLambdaQueryWrapper.eq(PuOrderShopEntity::getOrderCode, puOrderEntity.getCode());
            puOrderShopService.remove(shopLambdaQueryWrapper);
            puOrderShopService.saveBatch(orderShopEntityList);
        }
        this.saveOrUpdate(puOrderEntity);
        return true;
    }

    private void getQueryWrapper(PuOrderEntity puOrderEntity, LambdaQueryWrapper<PuOrderEntity> queryWrapper) {
        if (StringHelper.isNotEmpty(puOrderEntity.getOrderTimeStr())) {
            String orderTimeStr = puOrderEntity.getOrderTimeStr().trim();
            String[] split = orderTimeStr.split("~");
            queryWrapper.between(PuOrderEntity::getOrderTime, split[0], split[1]);
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getSupplierName())) {
            queryWrapper.like(PuOrderEntity::getSupplierName, puOrderEntity.getSupplierName());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getSupplierCode())) {
            queryWrapper.like(PuOrderEntity::getSupplierCode, puOrderEntity.getSupplierCode());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getPuUsername())) {
            queryWrapper.like(PuOrderEntity::getPuUsername, puOrderEntity.getPuUsername());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getPuAccount())) {
            queryWrapper.like(PuOrderEntity::getPuAccount, puOrderEntity.getPuAccount());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getStockName())) {
            queryWrapper.like(PuOrderEntity::getStockName, puOrderEntity.getStockName());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getStockCode())) {
            queryWrapper.like(PuOrderEntity::getStockCode, puOrderEntity.getStockCode());
        }
        if (ObjectUtils.isNotEmpty(puOrderEntity.getAmount())) {
            queryWrapper.eq(PuOrderEntity::getAmount, puOrderEntity.getAmount());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getApplyForCode())) {
            queryWrapper.like(PuOrderEntity::getApplyForCode, puOrderEntity.getApplyForCode());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getProjectCode())) {
            queryWrapper.like(PuOrderEntity::getProjectCode, puOrderEntity.getProjectCode());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getProjectName())) {
            queryWrapper.like(PuOrderEntity::getProjectName, puOrderEntity.getProjectName());
        }
        if (ObjectUtils.isNotEmpty(puOrderEntity.getStatus())) {
            queryWrapper.eq(PuOrderEntity::getStatus, puOrderEntity.getStatus());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getCreateName())) {
            queryWrapper.like(PuOrderEntity::getCreateName, puOrderEntity.getCreateName());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getUpdateName())) {
            queryWrapper.like(PuOrderEntity::getUpdateName, puOrderEntity.getUpdateName());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getCreateUser())) {
            queryWrapper.like(PuOrderEntity::getCreateUser, puOrderEntity.getCreateUser());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getRemarks())) {
            queryWrapper.like(PuOrderEntity::getRemarks, puOrderEntity.getRemarks());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getCheckName())) {
            queryWrapper.like(PuOrderEntity::getCheckName, puOrderEntity.getCheckName());
        }
        if (StringHelper.isNotEmpty(puOrderEntity.getCheckUser())) {
            queryWrapper.like(PuOrderEntity::getCheckUser, puOrderEntity.getCheckUser());
        }
    }
}

